package com.ruoyi.erecord.vehicles.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erecord.vehicles.domain.bo.VehiclesBo;
import com.ruoyi.erecord.vehicles.domain.vo.VehiclesVo;
import com.ruoyi.erecord.vehicles.service.IVehiclesService;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;

/**
 * 车辆
 *
 * @author cc
 * @date 2023-04-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erecord/vehicles")
public class VehiclesController extends BaseController {

    private final IVehiclesService iVehiclesService;
    
    /**
     * 查询车辆列表
     */
    @SaCheckPermission("erecord:vehicles:list")
    @GetMapping("/list")
    public TableDataInfo<VehiclesVo> list(VehiclesBo bo, PageQuery pageQuery) {
        return iVehiclesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出车辆列表
     */
    @SaCheckPermission("erecord:vehicles:export")
    @Log(title = "车辆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(VehiclesBo bo,PageQuery pageQuery, HttpServletResponse response) {
    	pageQuery.setPageSize(10000);
    	TableDataInfo<VehiclesVo> list = iVehiclesService.queryPageList(bo, pageQuery);
        ExcelUtil.exportExcel(list.getRows(), "车辆", VehiclesVo.class, response);
    }

    /**
     * 获取车辆详细信息
     *
     * @param vehiclesCode 主键
     */
    @SaCheckPermission("erecord:vehicles:query")
    @GetMapping("/{vehiclesCode}")
    public R<VehiclesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long vehiclesCode) {
        return R.ok(iVehiclesService.queryById(vehiclesCode));
    }

    /**
     * 新增车辆
     */
    @SaCheckPermission("erecord:vehicles:add")
    @Log(title = "车辆", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VehiclesBo bo) {
        return toAjax(iVehiclesService.insertByBo(bo));
    }

    /**
     * 修改车辆
     */
    @SaCheckPermission("erecord:vehicles:edit")
    @Log(title = "车辆", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VehiclesBo bo) {
        return toAjax(iVehiclesService.updateByBo(bo));
    }

    /**
     * 删除车辆
     *
     * @param vehiclesCodes 主键串
     */
    @SaCheckPermission("erecord:vehicles:remove")
    @Log(title = "车辆", businessType = BusinessType.DELETE)
    @DeleteMapping("/{vehiclesCodes}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] vehiclesCodes) {
        return toAjax(iVehiclesService.deleteWithValidByIds(Arrays.asList(vehiclesCodes), true));
    }
    
}
